package tw.com.hitevision.whiteboard.android.widgets;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import tw.com.hitevision.whiteboard.android.R;

public class QRCodeDialog extends ConstraintLayout {
    private final static String TAG = QRCodeDialog.class.getSimpleName();

    private View scrim;
    private View close;
    private ImageView qrCodeView;
    private ImageView qrCodeViewLarge;
    private View progressBar;

    public QRCodeDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public QRCodeDialog(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public QRCodeDialog(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.qrcode_dialog, this);

        OnClickListener closeListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                QRCodeDialog.this.setVisibility(GONE);
            }
        };

        scrim = findViewById(R.id.qr_code_scrim);
        close = findViewById(R.id.qr_code_close);
        qrCodeView = findViewById(R.id.qr_code_view);
        qrCodeViewLarge = findViewById(R.id.qr_code_view_large);
        progressBar = findViewById(R.id.qr_code_progress_bar);

        scrim.setOnClickListener(closeListener);
        close.setOnClickListener(closeListener);

        qrCodeView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleQrCodeSize();
            }
        });

        qrCodeViewLarge.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleQrCodeSize();
            }
        });

    }

    private void toggleQrCodeSize() {
        if (qrCodeView != null && qrCodeViewLarge != null) {
            if (qrCodeView.getVisibility() == VISIBLE) {
                qrCodeView.setVisibility(GONE);
                qrCodeViewLarge.setVisibility(VISIBLE);
            } else {
                qrCodeView.setVisibility(VISIBLE);
                qrCodeViewLarge.setVisibility(GONE);
            }
        }
    }

    public void setQrCodeView(Bitmap bitmap) {
        if (bitmap != null) {
            progressBar.setVisibility(GONE);
            qrCodeView.setVisibility(VISIBLE);
            qrCodeView.setImageBitmap(bitmap);
            qrCodeViewLarge.setImageBitmap(bitmap);
        } else {
            progressBar.setVisibility(VISIBLE);
            qrCodeView.setVisibility(VISIBLE);
            qrCodeView.setImageBitmap(null);
            qrCodeViewLarge.setImageBitmap(null);
        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == VISIBLE) {
            if (qrCodeView != null && qrCodeViewLarge != null) {
                qrCodeView.setVisibility(VISIBLE);
                qrCodeViewLarge.setVisibility(GONE);
            }
        }
    }

    private void log(String s) {
        Log.d(TAG, s);
    }
}
